/*
 *  Copyright (C) 2010 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.common.abstracts;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.balisunrise.annotation.FilterType;
import org.balisunrise.common.Criterionable;
import org.balisunrise.common.EntitySelection;
import org.balisunrise.common.FieldLayoutGroup;
import org.balisunrise.common.SelectionAction;
import org.balisunrise.common.SelectionLoader;

/**
 *
 * @author glauco
 */
public abstract class AbstractSelectionFieldLayout
        extends AbstractEmbeddedFieldLayout{

    protected FilterType filterType;
    protected boolean emptyForAll;
    protected Serializable entity;
    protected SelectionAction selectionAction;
    protected SelectionLoader selectionLoader;
    protected EntitySelection entitySelection;

    public static final String PROP_FILTERTYPE = "filterType";
    public static final String PROP_EMPTYFORALL = "emptyForAll";
    public static final String PROP_ENTITY = "entity";
    public static final String PROP_SELECTIONACTION = "selectionAction";
    public static final String PROP_SEELECTION_LOADER = "selectionLoader";
    public static final String PROP_ENTITY_SELECTION = "entitySelection";

    /**
     * retorna a Ação de seleção de entidades.
     * @return
     */
    public SelectionAction getSelectionAction() {
        return selectionAction;
    }

    /**
     * Atribui a Ação de seleção de entidades.
     * @param selectionAction
     */
    public void setSelectionAction(SelectionAction selectionAction) {
        SelectionAction oldSelectionAction = this.selectionAction;
        this.selectionAction = selectionAction;
        changeSupport.firePropertyChange(PROP_SELECTIONACTION,
                oldSelectionAction, selectionAction);
    }

    /**
     * Get the value of entity
     *
     * @return the value of entity
     */
    public Serializable getEntity() {
        return entity;
    }

    /**
     * Atribui a entidade selecionada.
     * @param entity
     */
    public void setEntity(Serializable entity) {
        Serializable oldEntity = this.entity;
        this.entity = entity;
        changeSupport.firePropertyChange(PROP_ENTITY, oldEntity, entity);
    }

    /**
     * Get the value of emptyForAll
     *
     * @return the value of emptyForAll
     */
    public boolean isEmptyForAll() {
        return emptyForAll;
    }

    /**
     * Set the value of emptyForAll
     *
     * @param emptyForAll new value of emptyForAll
     */
    public void setEmptyForAll(boolean emptyForAll) {
        boolean oldEmptyForAll = this.emptyForAll;
        this.emptyForAll = emptyForAll;
        changeSupport.firePropertyChange(PROP_EMPTYFORALL,
                oldEmptyForAll, emptyForAll);
    }

    /**
     * Get the value of filterType
     *
     * @return the value of filterType
     */
    public FilterType getFilterType() {
        return filterType;
    }

    /**
     * Set the value of filterType
     *
     * @param filterType new value of filterType
     */
    public void setFilterType(FilterType filterType) {
        FilterType oldFilterType = this.filterType;
        this.filterType = filterType;
        changeSupport.firePropertyChange(PROP_FILTERTYPE,
                oldFilterType, filterType);
    }

    /**
     * Atribui o EntitySelection deste SelectionPanelField
     *
     * @param entitySelection
     */
    public void setEntitySelection(EntitySelection entitySelection){
        EntitySelection old = this.entitySelection;
        this.entitySelection = entitySelection;
        changeSupport.firePropertyChange(PROP_ENTITY_SELECTION, old,
                entitySelection);
    }

    /**
     * Retorna o EntitySelection deste SelectionPanelField.
     *
     * @return
     */
    public EntitySelection getEntitySelection(){
        return entitySelection;
    }

    /**
     * Atribui o SelectionLoader deste SelectionPanelField.
     *
     * @param selectionAction
     */
    public void setSelectionLoader(SelectionLoader selectionLoader){
        SelectionLoader old = this.selectionLoader;
        this.selectionLoader = selectionLoader;
        changeSupport.firePropertyChange(PROP_SEELECTION_LOADER, old,
                selectionLoader);
    }

    /**
     * Retorna o SelectionLoader deste SelectionPanelField.
     *
     * @return
     */
    public SelectionLoader getSelectionLoader(){
        return selectionLoader;
    }

    /**
     * Retorna uma lista de Criterionables das objetos da lista de FieldLayouts.
     * @param flg Grupo de FieldLayout
     * @return uma lista de Criterionables.
     */
    protected List<Criterionable> extractCriterionables(FieldLayoutGroup flg){

        List<Criterionable> list = new ArrayList<Criterionable>();

        for (Object fl : flg.getFieldLayouts()) {
            if (fl instanceof Criterionable)
                list.add((Criterionable) fl);
            else if(fl instanceof FieldLayoutGroup)
                list.addAll(extractCriterionables((FieldLayoutGroup) fl));
        }

        return list;
    }
}
